package com.dnake.v700;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.dnake.Constant;
import com.dnake.WeKerApplication;
import com.dnake.entity.EventDeviceBean;
import com.dnake.misc.Sound;
import com.dnake.misc.SysAccess;
import com.dnake.misc.SysCard;
import com.dnake.misc.SysElev;
import com.dnake.misc.SysForward;
import com.dnake.misc.SysSpecial;
import com.dnake.misc.SysTalk;
import com.dnake.misc.SysWifi;
import com.dnake.misc.sCaller;
import com.dnake.panel.MainActivity;
import com.dnake.panel.WakeTask;

import org.greenrobot.eventbus.EventBus;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author haoyuliu
 * @since 2018/3/7
 */
public class devent {
    private static List<devent> elist = null;
    public static Boolean boot = false;
    public String url;

    public devent(String url) {
        this.url = url;
    }

    public void process(String xml) {
    }

    public static void event(String url, String xml) {
        Boolean err = true;
        if (boot && elist != null) {
            devent e;

            Iterator<devent> it = elist.iterator();
            while (it.hasNext()) {
                e = it.next();
                if (url.equals(e.url)) {
                    e.process(xml);
                    err = false;
                    break;
                }
            }
        }
        if (err)
            dmsg.ack(480, null);
    }

    public static void setup() {
        elist = new LinkedList<devent>();

        devent de;
        de = new devent("/ui/run") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);
            }
        };
        elist.add(de);

        de = new devent("/ui/version") {
            @Override
            public void process(String body) {
                dxml p = new dxml();
                String v = String.valueOf(sys.version_major)+"."+ sys.version_minor+"."+ sys.version_minor2;
                v = v+" "+ sys.version_date+" "+ sys.version_ex;
                p.setText("/params/version", v);
                p.setInt("/params/proxy", sys.qResult.sip.proxy);
                dmsg.ack(200, p.toString());
            }
        };
        elist.add(de);

        de = new devent("/ui/talk/start") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);

                sCaller.reset();
                sCaller.running = sCaller.RINGING;
                sCaller.mId = p.getText("/params/host");
                sCaller.refresh();

//				TalkLabel.mMode = TalkLabel.IN;
                SysTalk.start();
            }
        };
        elist.add(de);

        de = new devent("/ui/talk/stop") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);
                sCaller.bStop = true;
            }
        };
        elist.add(de);

        de = new devent("/ui/talk/play") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);
                SysTalk.play();
                sCaller.running = sCaller.TALK;
                sCaller.refresh();
            }
        };
        elist.add(de);

        de = new devent("/ui/sip/ringing") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);

                sCaller.mId = p.getText("/params/host");
                sCaller.running = sCaller.RINGING;
                sCaller.refresh();

//				TalkLabel.mMode = TalkLabel.OUT;
                SysTalk.start();
            }
        };
        elist.add(de);

        de = new devent("/ui/sip/register") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                sys.qResult.sip.proxy = p.getInt("/params/register", 0);
            }
        };
        elist.add(de);

        de = new devent("/ui/sip/query") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);
                if (sys.qResult.sip.url == null) {
                    dxml p = new dxml();
                    p.parse(body);
                    sys.qResult.sip.url = new String(p.getText("/params/url"));
                }
            }
        };
        elist.add(de);

        de = new devent("/ui/device/query") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                if (sys.qResult.d600.ip == null) {
                    dxml p = new dxml();
                    p.parse(body);
                    sys.qResult.d600.host = p.getText("/params/name");
                    sys.qResult.d600.ip = p.getText("/params/ip");
                }
            }
        };
        elist.add(de);

        de = new devent("/ui/sip/result") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                sys.qResult.result = p.getInt("/params/result", 0);
            }
        };
        elist.add(de);

        de = new devent("/ui/broadcast/data") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
            }
        };
        elist.add(de);

        de = new devent("/ui/ipwatchd") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                int result = p.getInt("/params/result", 0);
                String ip = p.getText("/params/ip");
                String mac = p.getText("/params/mac");
                SysTalk.ipMacErr(result, ip, mac);
            }
        };
        elist.add(de);

        de = new devent("/ui/touch/event") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                if (p.getInt("/params/data", -1) != -1) {
                    WakeTask.acquire();
                    if (p.getInt("/params/apk", 0) != 1)
                        SysTalk.tBroadcast();
                }
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/key") {
            private long ts = 0;
            @Override
            public void process(String body) {
                dmsg.ack(200, null);
                dxml p = new dxml();
                p.parse(body);
                String key = p.getText("/params/data");
                Log.e("测试", "按键: "+key );

//				if (key.charAt(0) == '*') {
//					if (Math.abs(System.currentTimeMillis()-ts) > 3*1000) {
//						dmsg req = new dmsg();
//						dxml p2 = new dxml();
//						p2.setInt("/params/key", 4);
//						req.to("/settings/key", p2.toString());
//					}
//					ts = System.currentTimeMillis();
//				}
                SysTalk.Keys.offer(key);
                SysTalk.touch(key);
            }
        };
        elist.add(de);
        de = new devent("/ui/ir/detect") { //人体感应
            private long mTs = 0;
            @Override
            public void process(String body) {
                Log.e("测试", "process: "+body);
                dmsg.ack(200, null);

//				if (sys.factory)
//					Sound.play(Sound.press, false);

                if (Math.abs(System.currentTimeMillis()-mTs) >= 20*1000) {
                    mTs = System.currentTimeMillis();
                    WakeTask.acquire();
                    SysTalk.tBroadcast();
                }
                if (!MainActivity.mOK){
                    Intent newIntent = new Intent(WeKerApplication.getContext(), MainActivity.class);
                    PendingIntent restartIntent = PendingIntent.getActivity(WeKerApplication.getContext(), 0, newIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    //退出程序
                    AlarmManager mgr = (AlarmManager)WeKerApplication.getContext().getSystemService(Context.ALARM_SERVICE);
                    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, restartIntent); // 1秒钟后重启应用
                    System.exit(0);
                }else {
                    EventBus.getDefault().post(new EventDeviceBean(Constant.EVENTBUS_FACE_DETECT,""));
                }
//				if (TalkLabel.mContext == null && !FaceLabel.mUsed && Math.abs(System.currentTimeMillis()-BaseLabel.mKeyTs) >= 35*1000) {
//					BaseLabel.mKeyTs = System.currentTimeMillis();
//					if (SysTalk.mContext != null && SysSpecial.face.enable != 0 && android.os.Build.VERSION.SDK_INT >= 19) {
//						FaceLabel.mMode = 0;
//						FaceLabel.mWaitTs = System.currentTimeMillis();
//						Intent it = new Intent(SysTalk.mContext, FaceLabel.class);
//						it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
//						SysTalk.mContext.startActivity(it);
//					}
//				}
            }
        };
        elist.add(de);
        de = new devent("/ui/v170/keyCall") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                if (p.getInt("/params/data", 0) == 0) { //按下
                    WakeTask.acquire();
                    SysTalk.Keys.offer("X");
                    SysTalk.touch("X");
                }
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/btn") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);
                dxml p = new dxml();
                p.parse(body);
                int btn = p.getInt("/params/btn", 0);
                int st = p.getInt("/params/data", 0);
                if (btn == 0) {
                    if (st == 0) //开门按钮
                        SysAccess.unlock(0);
                    com.orhanobut.logger.Logger.e("出门按钮");
                } else if (btn == 1) {
                    //门磁状态
                    //st 0:关门 1:开门
                    SysAccess.lock.sensor.ts = System.currentTimeMillis();
                    SysAccess.lock.sensor.st = st;
                    com.orhanobut.logger.Logger.e("门磁"+st);
                } else if (btn == 2) { //小门口机呼叫键
                    if (st == 0) {
                        SysTalk.Keys.offer("X");
                        SysTalk.touch("X");
                    }
                }
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/adc") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                int adc = p.getInt("/params/data", 0);
                System.err.println("adc: "+adc);
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/eio") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                int io = p.getInt("/params/io", 0);
                int d = p.getInt("/params/data", 0);
                System.err.println("eio: "+io+" "+d);
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/card") {
            @Override
            public void process(String body) {
                Log.e("测试", "process: "+body );
                dmsg.ack(200, null);
                WakeTask.acquire();

                dxml p = new dxml();
                p.parse(body);
                String card = p.getText("/params/data");
                if (card!=null){
                    //IC卡
                    EventBus.getDefault().post(new EventDeviceBean(Constant.EVENTBUS_IC_CARD,card));
                }else {
                    //身份证
                    card = p.getText("/params/datab");
                    EventBus.getDefault().post(new EventDeviceBean(Constant.EVENTBUS_IDENTITY_CARD,card));
                }
//				if (SysAccess.admin.card == 0) { //设置管理卡
//					SysAccess.admin.card = card;
//					SysAccess.save();
//					Sound.play(Sound.press, false);
//					return;
//				}
//				if (card == SysAccess.admin.card) { //管理卡加卡模式
//					SysAccess.admin.ts = System.currentTimeMillis();
//					Sound.play(Sound.press, false);
//					return;
//				}
//
//				sys.qResult.v170.card = card;
//				if (SysAccess.admin.ts != 0) { //管理卡添加
//					SysCard.Data d = new SysCard.Data();
//					d.card = card;
//					d.build = sys.talk.building;
//					d.unit = sys.talk.unit;
//					d.floor = 1;
//					d.family = 1;
//					SysCard.add(d);
//
//					SysAccess.admin.ts = System.currentTimeMillis();
//					Sound.play(Sound.modify_success, false);
//					return;
//				}
//
//				SysCard.Data d = SysCard.verify(card);
//				if (d != null) {
//					int b = 0, u = 0, f = 0, r = 0;
//					if (sys.panel.mode == 0) { //单元门口机
//						b = sys.talk.building;
//						u = sys.talk.unit;
//						f = d.floor;
//						r = d.family;
//					} else if (sys.panel.mode == 1) { //围墙机
//						b = d.build;
//						u = d.unit;
//						f = d.floor;
//						r = d.family;
//					} else if (sys.panel.mode == 2) { //小门口机
//						b = sys.talk.building;
//						u = sys.talk.unit;
//						f = sys.talk.floor;
//						r = sys.talk.family;
//					}
//
//					SysAccess.unlock(0);
//					SysAccess.logger(b, u, f, r, d.card);
//
//					if (SysAccess.security.enable != 0)
//						SysAccess.security(b, u, f, r, d.card);
//					if (sys.panel.mode == 1) //围墙机
//						SysAccess.elevWall(b, u, f, r);
//					else
//						SysAccess.elev(f, r);
//
//					sCaller.jpeg();
//				} else
//					Sound.play(Sound.card_err, false);
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/unlock") {
            @Override
            public void process(String body) {


                dmsg.ack(200, null);

                com.wx.logger.Logger.e("开门");
                if (sCaller.running >= sCaller.CALL && sCaller.running < sCaller.STOP) {
                    int id = 0;
                    if (sCaller.mId.length() < 12 && sCaller.mId.matches("[0-9]*"))
                        id = Integer.parseInt(sCaller.mId);

                    if (id != 0) {
                        int b = id/1000000;
                        int u = (id/10000)%100;
                        int f = (id/100)%100;
                        int r = id%100;
                        if (body != null && body.length() > 8) {
                            dxml p = new dxml();
                            p.parse(body);
                            b = p.getInt("/params/build", b);
                            u = p.getInt("/params/unit", u);
                            f = p.getInt("/params/floor", f);
                            r = p.getInt("/params/family", r);
                        }
                        if (sys.panel.mode == 0) //单元门口机
                            SysAccess.elev(f, r);
                        else if (sys.panel.mode == 1) //围墙机
                            SysAccess.elevWall(b, u, f, r);
                    }

                    sCaller.logger(sCaller.logger.UNLOCK);
                }

                dxml p = new dxml();
                p.parse(body);
                int index = p.getInt("/params/index", 0);
                SysAccess.unlock(index);
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/elev/appoint") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                int elev = p.getInt("/params/elev", 0);
                int direct = p.getInt("/params/direct", 0);
                int f = p.getInt("/params/floor", 0);
                int r = p.getInt("/params/family", 0);
                SysElev.appoint(elev, direct, f, r);
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/elev/permit") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                int elev = p.getInt("/params/elev", 0);
                int f = p.getInt("/params/floor", 0);
                int r = p.getInt("/params/family", 0);
                SysElev.permit(elev, f, r);
            }
        };
        elist.add(de);

        de = new devent("/ui/v170/elev/join") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                String url = p.getText("/params/url");
                SysElev.join(url);
            }
        };
        elist.add(de);

        de = new devent("/ui/center/card") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                SysCard.Data d = new SysCard.Data();
                d.card = p.getInt("/params/card", 0);
                d.build = p.getInt("/params/build", 0);
                d.unit = p.getInt("/params/unit", 0);
                d.floor = p.getInt("/params/floor", 0);
                d.family = p.getInt("/params/family", 0);
                if (p.getInt("/params/add", 1) == 1)
                    SysCard.add(d);
                else
                    SysCard.rm(d.card);
            }
        };
        elist.add(de);

        de = new devent("/ui/center/card2") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                int total = p.getInt("/params/total", 0);
                for(int i=0; i<total; i++) {
                    SysCard.Data d = new SysCard.Data();
                    d.card = p.getInt("/params/c"+i+"/d", 0);
                    if (p.getInt("/params/c"+i+"/a", 1) == 1) {
                        d.build = p.getInt("/params/c"+i+"/b", 0);
                        d.unit = p.getInt("/params/c"+i+"/u", 0);
                        d.floor = p.getInt("/params/c"+i+"/f", 0);
                        d.family = p.getInt("/params/c"+i+"/r", 0);
                        SysCard.add(d);
                    } else
                        SysCard.rm(d.card);
                }
            }
        };
        elist.add(de);

        de = new devent("/ui/web/voip/read") {
            @Override
            public void process(String body) {
                dxml p = new dxml();

                p.setText("/params/proxy", sys.sip.proxy);
                p.setText("/params/realm", sys.sip.realm);
                p.setText("/params/passwd", sys.sip.passwd);
                p.setText("/params/stun_ip", sys.sip.stun.ip);
                p.setInt("/params/stun_port", sys.sip.stun.port);
                p.setInt("/params/ex_enable", sys.sip.enable);
                p.setText("/params/ex_user", sys.sip.user);
                p.setText("/params/outbound", sys.sip.outbound);
                p.setInt("/params/timeout", sys.talk.timeout);
                p.setInt("/params/ringing", sys.panel.ringing);
                p.setInt("/params/host2id", sys.sip.host2id);
                p.setInt("/params/bitrate", sys.sip.bitrate);
                p.setInt("/params/payload/h264", sys.payload.H264);

                dmsg.ack(200, p.toString());
            }
        };
        elist.add(de);

        de = new devent("/ui/web/voip/write") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);

                String s = p.getText("/params/proxy");
                if (s != null) {
                    if (s.startsWith("sip:"))
                        sys.sip.proxy = s;
                    else
                        sys.sip.proxy = "sip:"+s;
                }
                sys.sip.realm = p.getText("/params/realm", sys.sip.realm);
                sys.sip.outbound = p.getText("/params/outbound", sys.sip.outbound);

                sys.sip.stun.ip = p.getText("/params/stun_ip", sys.sip.stun.ip);
                sys.sip.stun.port = p.getInt("/params/stun_port", sys.sip.stun.port);

                sys.sip.enable = p.getInt("/params/ex_enable", sys.sip.enable);
                sys.sip.user = p.getText("/params/ex_user", sys.sip.user);
                sys.sip.passwd = p.getText("/params/passwd", sys.sip.passwd);

                sys.talk.timeout = p.getInt("/params/timeout", 300);
                sys.panel.ringing = p.getInt("/params/ringing", 35);
                sys.sip.host2id = p.getInt("/params/host2id", 1);
                sys.sip.bitrate = p.getInt("/params/bitrate", 768000);
                sys.payload.H264 = p.getInt("/params/payload/h264", 102);

                sys.save();
            }
        };
        elist.add(de);

        de = new devent("/ui/web/device/read") {
            @Override
            public void process(String body) {
                dxml p = new dxml();

                p.setText("/params/passwd", sys.admin.passwd);

                p.setInt("/params/build", sys.talk.building);
                p.setInt("/params/unit", sys.talk.unit);
                p.setInt("/params/floor", sys.talk.floor);
                p.setInt("/params/family", sys.talk.family);

                p.setInt("/params/index", sys.panel.index);
                p.setInt("/params/mode", sys.panel.mode);
                p.setInt("/params/nUnit", sys.panel.nUnit);
                p.setInt("/params/forward", sys.panel.forward);

                p.setInt("/params/volume", Sound.volume());
                p.setInt("/params/lang", sys.language());
                p.setInt("/params/video/format", sys.video.format);

                dmsg.ack(200, p.toString());
            }
        };
        elist.add(de);

        de = new devent("/ui/web/device/write") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);

                sys.admin.passwd = p.getText("/params/passwd", sys.admin.passwd);

                sys.talk.building = p.getInt("/params/build", sys.talk.building);
                sys.talk.unit = p.getInt("/params/unit", sys.talk.unit);
                sys.talk.floor = p.getInt("/params/floor", sys.talk.floor);
                sys.talk.family = p.getInt("/params/family", sys.talk.family);

                sys.panel.index = p.getInt("/params/index", sys.panel.index);
                sys.panel.mode = p.getInt("/params/mode", sys.panel.mode);
                sys.panel.nUnit = p.getInt("/params/nUnit", sys.panel.nUnit);
                sys.panel.forward = p.getInt("/params/forward", sys.panel.forward);
                sys.video.format = p.getInt("/params/video/format", sys.video.format);

                Sound.volume(p.getInt("/params/volume", 5));
                sys.language(p.getInt("/params/lang", 0));

                sys.save();
            }
        };
        elist.add(de);

        de = new devent("/ui/web/access/read") {
            @Override
            public void process(String body) {
                dxml p = new dxml();

                for(int i=0; i<8; i++)
                    p.setText("/params/lock/passwd_"+i, SysAccess.lock.passwd[i]);

                p.setInt("/params/lock/timeout", SysAccess.lock.timeout);
                p.setInt("/params/lock/delay", SysAccess.lock.delay);

                p.setInt("/params/admin/card", SysAccess.admin.card);
                p.setInt("/params/elev_to", SysAccess.elev.to);
                p.setInt("/params/security/enable", SysAccess.security.enable);

                dmsg.ack(200, p.toString());
            }
        };
        elist.add(de);

        de = new devent("/ui/web/access/write") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);

                for(int i=0; i<8; i++)
                    SysAccess.lock.passwd[i] = p.getText("/params/lock/passwd_"+i);

                SysAccess.lock.timeout = p.getInt("/params/lock/timeout", SysAccess.lock.timeout);
                SysAccess.lock.delay = p.getInt("/params/lock/delay", SysAccess.lock.delay);

                SysAccess.admin.card = p.getInt("/params/admin/card", SysAccess.admin.card);
                SysAccess.elev.to = p.getInt("/params/elev_to", SysAccess.elev.to);
                SysAccess.security.enable = p.getInt("/params/security/enable", SysAccess.security.enable);

                SysAccess.save();
            }
        };
        elist.add(de);

        de = new devent("/ui/web/network/read") {
            @Override
            public void process(String body) {
                dxml p = new dxml();

                p.setText("/params/server", sys.talk.server);
                p.setText("/params/passwd", sys.talk.passwd);

                dmsg.ack(200, p.toString());
            }
        };
        elist.add(de);

        de = new devent("/ui/web/network/write") {
            @Override
            public void process(String body) {
                dmsg.ack(200, null);

                dxml p = new dxml();
                p.parse(body);
                sys.talk.server = p.getText("/params/server", sys.talk.server);
                sys.talk.passwd = p.getText("/params/passwd", sys.talk.passwd);
                sys.save();
            }
        };
        elist.add(de);

        de = new devent("/ui/web/forward/read") {
            @Override
            public void process(String body) {
                dxml p = new dxml();
                p.parse(body);
                int id = p.getInt("/params/id", 0);

                p.setInt("/params/id", id);
                p.setText("/params/data", SysForward.get(id));

                dmsg.ack(200, p.toString());
            }
        };
        elist.add(de);

        de = new devent("/ui/web/forward/write") {
            @Override
            public void process(String body) {
                dxml p = new dxml();
                p.parse(body);
                int id = p.getInt("/params/id", 0);
                String data = p.getText("/params/data");
                SysForward.set(id, data);
                SysForward.save();

                dmsg.ack(200, null);
            }
        };
        elist.add(de);

        de = new devent("/ui/web/advanced/read") {
            @Override
            public void process(String body) {
                dxml p = new dxml();
                p.setInt("/params/speed_dial", SysSpecial.speed_dial);

                p.setInt("/params/quick/enable", SysSpecial.quick.enable);
                p.setText("/params/quick/url", SysSpecial.quick.url);

                p.setInt("/params/pstn/enable", SysSpecial.pstn.enable);
                p.setText("/params/pstn/url", SysSpecial.pstn.url);

                p.setInt("/params/onu_arp", sys.talk.onu_arp);
                p.setInt("/params/feed/max", sys.feed.max);
                for(int i = 0; i< sys.feed.max; i++) {
                    p.setText("/params/feed/url"+i, sys.feed.url[i]);
                }

                dmsg.ack(200, p.toString());
            }
        };
        elist.add(de);

        de = new devent("/ui/web/advanced/write") {
            @Override
            public void process(String body) {
                dxml p = new dxml();
                p.parse(body);

                SysSpecial.speed_dial = p.getInt("/params/speed_dial", 0);

                SysSpecial.quick.enable = p.getInt("/params/quick/enable", 0);
                SysSpecial.quick.url = p.getText("/params/quick/url");

                SysSpecial.pstn.enable = p.getInt("/params/pstn/enable", 0);
                SysSpecial.pstn.url = p.getText("/params/pstn/url");

                SysSpecial.save();

                sys.talk.onu_arp = p.getInt("/params/onu_arp", 0);
                sys.feed.max = p.getInt("/params/feed/max", 0);
                for(int i = 0; i< sys.feed.max; i++) {
                    sys.feed.url[i] = p.getText("/params/feed/url"+i);
                }
                sys.save();

                dmsg.ack(200, null);
            }
        };
        elist.add(de);

        de = new devent("/ui/web/wireless/read") {
            @Override
            public void process(String body) {
                dxml p = new dxml();
                p.setInt("/params/wifi/enabled", SysWifi.mEnabled);
                p.setInt("/params/wifi/mode", SysWifi.mMode);
                p.setText("/params/wifi/ssid", SysWifi.mSSID);
                p.setText("/params/wifi/passwd", SysWifi.mPasswd);
                dmsg.ack(200, p.toString());
            }
        };
        elist.add(de);

        de = new devent("/ui/web/wireless/write") {
            @Override
            public void process(String body) {
                dxml p = new dxml();
                p.parse(body);
                SysWifi.mEnabled = p.getInt("/params/wifi/enabled", 0);
                SysWifi.mMode = p.getInt("/params/wifi/mode", 0);
                SysWifi.mSSID = p.getText("/params/wifi/ssid");
                SysWifi.mPasswd = p.getText("/params/wifi/passwd");
                SysWifi.save();
                dmsg.ack(200, null);
            }
        };
        elist.add(de);
    }
}
